// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    //extends: cc.Component,
    __ctor__: function (row, column) {
        //this.type = Math.floor(cc.random0To1() * 4);
        this.type = 0;
        this.row = row;
        this.column = column;
        this.backGround = cc.find("Canvas/PuzzleBG");
        this.content = cc.find("Canvas/PuzzleChooseScrollView/view/content");
        this.main = cc.find("Canvas").getComponent("Main");
        //var self = this;
        console.log("struct");
    },
    properties: {
        //行
        row: 0,
        //列
        column: 0,
        //当前拼图资源
        texture: {
            default: null,
            url: cc.Texture2D,
        },
        //遮罩图集
        spriteAtlas: {
            default: null,
            type: cc.SpriteAtlas,
        },
        //所有小块数组
        spriteArray: {
            default: null,
        },
        //废片数组
        useless: {
            default: null,
        },
        //排列类型
        type: {
            default: -1,  //初始-1, 0：不做翻转，1：左右翻转，2：上下翻转，3：对角线翻转
        },
        //背景板
        backGround: {
            default: null,
        },
        //一共多少块
        num: {
            get() {
                return this._bar;
            },
            set(value) {
                this._bar = value;
                switch (this.type) {
                    case 0:
                        this.spriteArray = new Array(this.row);
                        for (let i = 0; i < this.spriteArray.length; i++) {
                            this.spriteArray[i] = new Array(this.column);
                            for (let j = 0; j < this.spriteArray[i].length; j++) {
                                let id = this.num - i * this.column - (this.column - 1 - j);  //id对应图片名
                                let index = (i * 6 + j + 1).toString();       //图片名1-48
                                let name = (i + 1) + "_" + (j + 1);
                                let spriteFrame = this.spriteAtlas.getSpriteFrame(id.toString());
                                let newNode = new cc.Node(name);
                                newNode.width = spriteFrame.getOriginalSize().width;
                                newNode.height = spriteFrame.getOriginalSize().height;
                                let backNode = cc.instantiate(newNode); //背景位置节点
                                let childNode = cc.instantiate(newNode);
                                backNode.setParent(this.backGround);
                                let posx = defaultList[this.num][this.row - i - 1][j].x + newNode.width / 2;
                                let posy = defaultList[this.num][this.row - i - 1][j].y + newNode.height / 2;
                                backNode.setPosition(posx, posy);
                                let mask = newNode.addComponent(cc.Mask);
                                mask.type = cc.Mask.Type.IMAGE_STENCIL;
                                mask.spriteFrame = spriteFrame;
                                mask.alphaThreshold = 0.5;
                                //mask.resize();
                                newNode.addChild(childNode);
                                let sprite = childNode.addComponent(cc.Sprite);
                                let pixelx = defaultList[this.num][this.row - i - 1][j].x;
                                let pixely = 960 - defaultList[this.num][this.row - i - 1][j].y - newNode.height;
                                let frame = new cc.SpriteFrame(this.texture, cc.rect(pixelx, pixely, newNode.width, newNode.height), false, cc.v2(0, 0), cc.v2(newNode.width, newNode.height));
                                sprite.spriteFrame = frame;
                                console.log(sprite.spriteFrame);
                                this.spriteArray[i][j] = { name: index, row: i + 1, column: j + 1, node: newNode };
                                // this.spriteArray[i][j].node.setParent(this.content);
                                // let drag = this.spriteArray[i][j].node.addComponent("Drag");
                                // drag.width = i + 1;
                                // drag.height = j + 1;
                                // drag.name = name;
                                // this.spriteArray[i][j].node.position = cc.v2(-2900 + index * 200, 20);
                                this.spriteArray[i][j].node.setParent(this.backGround);
                                this.spriteArray[i][j].node.position = backNode.position;
                                //console.log(backNode.name);
                            }
                        }
                        break;
                    case 1:
                        this.spriteArray = new Array(this.row);
                        for (let i = 0; i < this.spriteArray.length; i++) {
                            this.spriteArray[i] = new Array(this.column);
                            for (let j = 0; j < this.spriteArray[i].length; j++) {
                                let id = this.num - i * this.column - j;  //id对应图片名
                                let index = (i * 6 + j + 1).toString();       //图片名
                                let name = (i + 1) + "_" + (j + 1);
                                let spriteFrame = this.spriteAtlas.getSpriteFrame(id.toString());
                                let newNode = new cc.Node(name);
                                newNode.width = spriteFrame.getOriginalSize().width;
                                newNode.height = spriteFrame.getOriginalSize().height;
                                newNode.setScale(-1, 1);
                                let backNode = cc.instantiate(newNode); //背景位置节点
                                let childNode = cc.instantiate(newNode);
                                backNode.setParent(this.backGround);
                                let posx = 720 - defaultList[this.num][this.row - i - 1][this.column - j - 1].x - newNode.width / 2;
                                let posy = defaultList[this.num][this.row - i - 1][this.column - j - 1].y + newNode.height / 2;
                                backNode.setPosition(posx, posy);
                                let mask = newNode.addComponent(cc.Mask);
                                mask.type = cc.Mask.Type.IMAGE_STENCIL;
                                mask.spriteFrame = spriteFrame;
                                mask.alphaThreshold = 0.5;
                                //mask.resize();
                                newNode.addChild(childNode);
                                let sprite = childNode.addComponent(cc.Sprite);
                                let pixelx = 720 - defaultList[this.num][this.row - i - 1][this.column - j - 1].x - newNode.width;
                                let pixely = 960 - defaultList[this.num][this.row - i - 1][this.column - j - 1].y - newNode.height;
                                let frame = new cc.SpriteFrame(this.texture, cc.rect(pixelx, pixely, newNode.width, newNode.height), false, cc.v2(0, 0), cc.size(newNode.width, newNode.height));
                                sprite.spriteFrame = frame;
                                this.spriteArray[i][j] = { name: index, row: i + 1, column: j + 1, node: newNode };
                                this.spriteArray[i][j].node.setParent(this.content);
                                let drag = this.spriteArray[i][j].node.addComponent("Drag");
                                drag.width = i + 1;
                                drag.height = j + 1;
                                drag.name = name;
                                // this.spriteArray[i][j].node.position = cc.v2(-2900 + index * 200, 20);
                                // this.spriteArray[i][j].node.setParent(this.backGround);
                                // this.spriteArray[i][j].node.position = backNode.position;
                                // console.log(backNode.name);
                            }
                        }
                        break;
                    case 2:
                        this.spriteArray = new Array(this.row);
                        for (let i = 0; i < this.spriteArray.length; i++) {
                            this.spriteArray[i] = new Array(this.column);
                            for (let j = 0; j < this.spriteArray[i].length; j++) {
                                let id = i * this.column + j + 1;  //id对应图片名
                                let index = (i * 6 + j + 1).toString();       //图片名
                                let name = (i + 1) + "_" + (j + 1);
                                let spriteFrame = this.spriteAtlas.getSpriteFrame(id.toString());
                                let newNode = new cc.Node(name);
                                newNode.width = spriteFrame.getOriginalSize().width;
                                newNode.height = spriteFrame.getOriginalSize().height;
                                newNode.setScale(1, -1);
                                let backNode = cc.instantiate(newNode); //背景位置节点
                                let childNode = cc.instantiate(newNode);
                                backNode.setParent(this.backGround);
                                let posx = defaultList[this.num][i][j].x + newNode.width / 2;
                                let posy = 960 - defaultList[this.num][i][j].y - newNode.height / 2;
                                backNode.setPosition(posx, posy);
                                let mask = newNode.addComponent(cc.Mask);
                                mask.type = cc.Mask.Type.IMAGE_STENCIL;
                                mask.spriteFrame = spriteFrame;
                                mask.alphaThreshold = 0.5;
                                //mask.resize();
                                newNode.addChild(childNode);
                                let sprite = childNode.addComponent(cc.Sprite);
                                let pixelx = defaultList[this.num][i][j].x;
                                let pixely = defaultList[this.num][i][j].y;
                                let frame = new cc.SpriteFrame(this.texture, cc.rect(pixelx, pixely, newNode.width, newNode.height), false, cc.v2(0, 0), cc.size(newNode.width, newNode.height));
                                sprite.spriteFrame = frame;
                                this.spriteArray[i][j] = { name: index, row: i + 1, column: j + 1, node: newNode };
                                this.spriteArray[i][j].node.setParent(this.content);
                                let drag = this.spriteArray[i][j].node.addComponent("Drag");
                                drag.width = i + 1;
                                drag.height = j + 1;
                                drag.name = name;
                                // this.spriteArray[i][j].node.position = cc.v2(-2900 + index * 200, 20);
                                // this.spriteArray[i][j].node.setParent(this.backGround);
                                // this.spriteArray[i][j].node.position = backNode.position;
                                // console.log(backNode.name);
                            }
                        }
                        break;
                    case 3:
                        this.spriteArray = new Array(this.row);
                        for (let i = 0; i < this.spriteArray.length; i++) {
                            this.spriteArray[i] = new Array(this.column);
                            for (let j = 0; j < this.spriteArray[i].length; j++) {
                                let id = this.num - i * this.column - j;  //id对应图片名
                                let index = (i * 6 + j + 1).toString();       //图片名1-48
                                let name = (i + 1) + "_" + (j + 1);
                                let spriteFrame = this.spriteAtlas.getSpriteFrame(id.toString());
                                let newNode = new cc.Node(name);
                                newNode.width = spriteFrame.getOriginalSize().width;
                                newNode.height = spriteFrame.getOriginalSize().height;
                                newNode.setScale(-1, -1);
                                let backNode = cc.instantiate(newNode); //背景位置节点
                                let childNode = cc.instantiate(newNode);
                                backNode.setParent(this.backGround);
                                let posx = 720 - defaultList[this.num][this.row - i - 1][this.column - j - 1].x - newNode.width / 2;
                                let posy = 960 - defaultList[this.num][this.row - i - 1][this.column - j - 1].y - newNode.height / 2;
                                backNode.setPosition(posx, posy);
                                let mask = newNode.addComponent(cc.Mask);
                                mask.type = cc.Mask.Type.IMAGE_STENCIL;
                                mask.spriteFrame = spriteFrame;
                                mask.alphaThreshold = 0.5;
                                //mask.resize();
                                newNode.addChild(childNode);
                                let sprite = childNode.addComponent(cc.Sprite);
                                let pixelx = 720 - defaultList[this.num][this.row - i - 1][this.column - j - 1].x - newNode.width;
                                let pixely = defaultList[this.num][this.row - i - 1][this.column - j - 1].y;
                                let frame = new cc.SpriteFrame(this.texture, cc.rect(pixelx, pixely, newNode.width, newNode.height), false, cc.v2(0, 0), cc.size(newNode.width, newNode.height));
                                sprite.spriteFrame = frame;
                                this.spriteArray[i][j] = { name: index, row: i + 1, column: j + 1, node: newNode };
                                this.spriteArray[i][j].node.setParent(this.content);
                                let drag = this.spriteArray[i][j].node.addComponent("Drag");
                                drag.width = i + 1;
                                drag.height = j + 1;
                                drag.name = name;
                                // this.spriteArray[i][j].node.position = cc.v2(-2900 + index * 200, 20);
                                // this.spriteArray[i][j].node.setParent(this.backGround);
                                // this.spriteArray[i][j].node.position = backNode.position;
                                // console.log(backNode.name);
                            }
                        }
                        break;

                    default:
                        break;
                }
            }
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {
        var index = Math.floor(cc.random0To1() * 4) + 1;
        var self = this;
        cc.loader.loadRes("background/img" + index, cc.Texture2D, function (err, texture) {
            self.texture = texture;
            console.log(texture);
            cc.loader.loadRes("Atlas/atlas" + self.row * self.column, cc.SpriteAtlas, function (err, atlas) {
                console.log(atlas);
                self.spriteAtlas = atlas;
                self.num = self.row * self.column;
                //self.generateUseless(self.column);
                //self.spriteArray[0][0].node.setPosition(cc.v2(-100, -100));
                // for (let i = 0; i < self.row; i++) {
                //     for (let j = 0; j < self.column; j++) {
                //         self.main.startpuzzle.push(self.spriteArray[i][j].node);
                        
                //     }
                // }
                // for (let i = 0; i < self.useless.length; i++) {
                //     self.main.startpuzzle.push(self.useless[i].node);
                    
                // }
                // self.main.startpuzzle = self.sortAndSetPosition(self.main.startpuzzle);
                // console.log(self.content.getChildren().length);
            });
        });
    },

    // update (dt) {},

    //生成废片
    generateUseless(num) {
        this.useless = new Array(num);
        for (let i = 0; i < num; i++) {
            let id = Math.floor(cc.random0To1() * this.num) + 1;  //id对应图片名
            //let name = (this.num + i + 1).toString();       //图片名
            let name = "0_0";
            let spriteFrame = this.spriteAtlas.getSpriteFrame(id.toString());
            let newNode = new cc.Node(name);
            newNode.width = spriteFrame.getOriginalSize().width;
            newNode.height = spriteFrame.getOriginalSize().height;
            //let backNode = cc.instantiate(newNode); //背景位置节点
            let childNode = cc.instantiate(newNode);
            //backNode.setParent(this.backGround);
            //backNode.setPosition(defaultList[this.num][this.row - i - 1][j].x + newNode.width / 2, defaultList[this.num][this.row - i - 1][j].y + newNode.height / 2);
            let mask = newNode.addComponent(cc.Mask);
            mask.type = cc.Mask.Type.IMAGE_STENCIL;
            mask.spriteFrame = spriteFrame;
            mask.alphaThreshold = 0.5;
            //mask.resize();
            newNode.addChild(childNode);
            let sprite = childNode.addComponent(cc.Sprite);
            let posx = Math.floor(cc.random0To1() * 320) + 200;
            let posy = Math.floor(cc.random0To1() * 560) + 200;
            let frame = new cc.SpriteFrame(this.texture, cc.rect(posx, posy, newNode.width, newNode.height), false, cc.v2(0, 0), cc.size(newNode.width, newNode.height));
            sprite.spriteFrame = frame;
            this.useless[i] = { name: name, row: -1, column: -1, node: newNode };
            this.useless[i].node.setParent(this.content);
            let drag = this.useless[i].node.addComponent("Drag");
            drag.width = -1;
            drag.height = -1;
            drag.name = "0_0";
            //this.useless[i].node.position = cc.v2((i + 48) * 200, 20);
            //this.useless[i].node.position = cc.v2(i * 200, 20);

        }
    },
    //排序和定位置
    sortAndSetPosition(array) {
        let newArray = new Array();
        for (let i = 0; i < this.num + this.useless.length; i++) {
            let ran = Math.floor(cc.random0To1() * array.length);
            newArray.push(array[ran]);
            array.splice(ran, 1);
            console.log(newArray[i]);
            //newArray[i].position = cc.v2(100 + i * 200, 0);
            newArray[i].position = cc.v2(-100, -100);
        }
        return newArray;
    },
});

var defaultList = {
    48: [
        [
            { x: 0, y: 807 },
            { x: 112, y: 835 },
            { x: 210, y: 811 },
            { x: 352, y: 839 },
            { x: 473, y: 808 },
            { x: 567, y: 829 },
        ],
        [
            { x: 0, y: 711 },
            { x: 87, y: 711 },
            { x: 206, y: 713 },
            { x: 331, y: 717 },
            { x: 469, y: 687 },
            { x: 589, y: 684 },
        ],
        [
            { x: 0, y: 593 },
            { x: 91, y: 592 },
            { x: 231, y: 593 },
            { x: 362, y: 594 },
            { x: 470, y: 592 },
            { x: 588, y: 595 },
        ],
        [
            { x: 0, y: 450 },
            { x: 89, y: 443 },
            { x: 237, y: 475 },
            { x: 329, y: 471 },
            { x: 477, y: 451 },
            { x: 565, y: 455 },
        ],
        [
            { x: 0, y: 361 },
            { x: 110, y: 353 },
            { x: 208, y: 329 },
            { x: 351, y: 337 },
            { x: 475, y: 357 },
            { x: 565, y: 353 },
        ],
        [
            { x: 0, y: 211 },
            { x: 88, y: 239 },
            { x: 207, y: 232 },
            { x: 358, y: 204 },
            { x: 445, y: 230 },
            { x: 589, y: 204 },
        ],
        [
            { x: 0, y: 91 },
            { x: 118, y: 120 },
            { x: 232, y: 98 },
            { x: 331, y: 92 },
            { x: 473, y: 118 },
            { x: 571, y: 118 },
        ],
        [
            { x: 0, y: 0 },
            { x: 86, y: 0 },
            { x: 236, y: 0 },
            { x: 351, y: 0 },
            { x: 445, y: 0 },
            { x: 595, y: 0 },
        ],
    ],
    35: [],
    24: [],
}
